package com.example.app.pages.main.home.addNew.addRecord.MyModalBottomSheetRelationPicker

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.example.app.addNewViewModel
import com.example.app.components.MyButton.MyTextButton
import com.example.app.components.MyDialog.MyAlertDialog
import com.example.app.components.MyDivider.MyDivider
import com.example.app.components.MyDivider.MyDividerDirection
import com.example.app.components.MyEmptyShow.MyEmptyShow
import com.example.app.components.MyNotify.MyNotify
import com.example.app.components.MyNotify.MyNotifyType
import com.example.app.components.MyScaffold.MyCustomScaffold
import com.example.app.components.MyText.MyText
import com.example.app.data.bean.routeAddAccount.AccountRelationPeople
import com.example.app.pages.main.home.me.viewModel.AddNewViewModel
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.launch

enum class ManageRelationListDeleteType(var type: String) {
    DELETE_ONE("DELETE_ONE"),
    DELETE_MANEY("DELETE_MANEY"),
}
//关系人列表
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ManageRelationList(
        notifyShow: MutableState<Boolean>,
        notifyType: MutableState<MyNotifyType>,
        notifyContent: MutableState<String>,

        animateOffsetX: Dp,
        windowWidth: Dp,
        dismiss: () -> Unit,
        addRelationPeopleHandler: () -> Unit,
        editSelectedCurRelationPeopleHandler: (curEditAccountRelationPeople: AccountRelationPeople) -> Unit,
) {
    var scope = rememberCoroutineScope()
    //编辑列表
    var isEdit = remember {
        mutableStateOf(false)
    }
    var showConfirmDialog = remember {
        mutableStateOf(false)
    }
    var deleteType = remember {
        mutableStateOf(ManageRelationListDeleteType.DELETE_ONE)
    }
    var curDeleteRelationPeople: AccountRelationPeople? = null
    //确认删除对话框
    MyAlertDialog(
        show = showConfirmDialog,
        onDismissRequest = {
            showConfirmDialog.value = false
        },
        confirmButton = {
            MyTextButton(onClick = {
                when (deleteType.value.type) {
                    //单个删除
                    ManageRelationListDeleteType.DELETE_ONE.type -> {
                        if (curDeleteRelationPeople != null) {
                            //执行删除
                            scope.launch {
                                var result =
                                    addNewViewModel.request_deleteAccountRelationPeople(
                                        DefineType = curDeleteRelationPeople!!.DefineType.toString(),
                                        AccountRelationId = curDeleteRelationPeople!!._id.toString()
                                    ).await()
                                if (result.code < 1) {
                                    addNewViewModel.updateAccountRelationPeopleList()
                                    curDeleteRelationPeople = null
                                    showConfirmDialog.value = false
                                    notifyContent.value = "删除成功"
                                    notifyShow.value = true
                                }
                            }
                        }
                    }
                    //批量删除
                    ManageRelationListDeleteType.DELETE_MANEY.type -> {
                        scope.launch {
                            var checkedItems =
                                addNewViewModel.checkBoxListIncomeRelationPeopleItemList.filter { it.checked }
                            //promise all 串行执行任务
                            addNewViewModel.request_deleteManyAccountRelationPeoples(
                                list = checkedItems
                                    .map {
                                        it.relationPeople
                                    }.toMutableList()
                            ).awaitAll()
                            addNewViewModel.updateAccountRelationPeopleList()
                            curDeleteRelationPeople = null
                            showConfirmDialog.value = false
                            notifyContent.value = "删除成功"
                            notifyShow.value = true
                        }
                    }

                    else -> {
                    }
                }
            }) {
                MyText(text = "确定")
            }
        },
        title = {
            MyText(text = "提示")
        },
        text = {
            MyText(text = "确定要删除吗？")
        }
    )
    MyCustomScaffold(
        modifier = Modifier
            .offset(x = animateOffsetX)
            .width(windowWidth)
            .background(MaterialTheme.colorScheme.background),
        topBar = {
            ManageRelationListTopBar(
                addNewViewModel,
                isEdit,
                dismiss
            )
            MyNotify(
                show = notifyShow,
                content = notifyContent
            )
        },
        topBarDivider = {
            MyDivider(myDividerDirection = MyDividerDirection.HOR)
        },
        content = {
            if (addNewViewModel.checkBoxListIncomeRelationPeopleItemList.size > 0) {
                LazyColumn(
                    modifier = Modifier
                        .height(with(LocalDensity.current) {
                            LocalConfiguration.current.screenHeightDp
                        }.dp - TopAppBarDefaults.TopAppBarExpandedHeight * 2)
                ) {
                    itemsIndexed(addNewViewModel.checkBoxListIncomeRelationPeopleItemList) { index, myCustomCheckBoxItem ->
                        AccountRelationPeopleItem(
                            myCustomCheckBoxItem = myCustomCheckBoxItem,
                            accountRelationPeople = myCustomCheckBoxItem.relationPeople,
                            isEdit = isEdit,
                            editSelectedCurRelationPeopleHandler = {
                                editSelectedCurRelationPeopleHandler(it)
                            },
                            deleteRelationPeopleItemHandler = {
                                curDeleteRelationPeople = it
                                deleteType.value = ManageRelationListDeleteType.DELETE_ONE
                                showConfirmDialog.value = true
                            },
                            checkHandler = {
                                addNewViewModel.checkBoxListIncomeRelationPeopleItemList[index] =
                                    addNewViewModel.checkBoxListIncomeRelationPeopleItemList[index].copy(checked = it)
                            }
                        )
                    }
                }
            } else {
                MyEmptyShow()
            }
        },
        bottomBar = {
            //新增关系人
            ManageIncomeRelationAdd(
                isEdit = isEdit,
                addNewViewModel = addNewViewModel,
                clickHandler = addRelationPeopleHandler,
                showConfirmDialog = {
                    deleteType.value = ManageRelationListDeleteType.DELETE_MANEY
                    showConfirmDialog.value = true
                },
                selectAll = {
                    var hasUnSelected =
                        addNewViewModel.checkBoxListIncomeRelationPeopleItemList.find { it.checked == false }
                    //全选true
                    if (hasUnSelected != null) {
                        addNewViewModel.checkBoxListIncomeRelationPeopleItemList.forEachIndexed { index, myCustomCheckBoxItem ->
                            addNewViewModel.checkBoxListIncomeRelationPeopleItemList[index] =
                                myCustomCheckBoxItem.copy(checked = true)
                        }
                    }
                    //反选false
                    else {
                        addNewViewModel.checkBoxListIncomeRelationPeopleItemList.forEachIndexed { index, myCustomCheckBoxItem ->
                            addNewViewModel.checkBoxListIncomeRelationPeopleItemList[index] =
                                myCustomCheckBoxItem.copy(checked = false)
                        }
                    }
                }
            )
        }
    )
}






